package com.paomi.client.utils;



import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Created by pc on 2017/6/15.
 */
public class Datas {
    /**
     * 获取timestamp 当天日期
     */
    public static String getTimestamp(){
        Calendar c=Calendar.getInstance();
        int day=c.get(Calendar.DAY_OF_MONTH);



        int mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份
        int mDay = c.get(Calendar.DAY_OF_MONTH);// 获取当日期
        int mWay = c.get(Calendar.DAY_OF_WEEK);// 获取当前日期的星期
        int mHour = c.get(Calendar.HOUR_OF_DAY);//时
        int mMinute = c.get(Calendar.MINUTE);//分


        return day+"";
    }

    /**
     * 获取当前月份
     * @return
     */
    public static int getCalendarYear(){
        Calendar c=Calendar.getInstance();
        int mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份
        return mMonth;
    }

    /**
     * 获取当前星期
     * @return
     */
    public static int getCalendarXq(){
        Calendar c=Calendar.getInstance();
        int mWay = c.get(Calendar.DAY_OF_WEEK);// 获取当前日期的星期
        return mWay;
    }


    /**
     * 根据日期获得所在周的日期
     * @param mdate
     * @return
     */
    @SuppressWarnings("deprecation")
    public static List<Date> dateToWeek(Date mdate) {
        int b = mdate.getDay();
        Date fdate;
        List<Date> list = new ArrayList<Date>();
        Long fTime = mdate.getTime() - b * 24 * 3600000;
        for (int a = 1; a <= 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(a-1, fdate);
        }
        return list;
    }
    /**
     * 根据日期获得星期
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        String[] weekDaysCode = { "7","1", "2", "3", "4", "5", "6" };
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDaysName[intWeek];
    }
    public static String getTimes(){
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String date = sDateFormat.format(new Date());
        return date;
    }
    public static String getTimesYue(){
        SimpleDateFormat sDateFormat = new SimpleDateFormat("MM");
        String date = sDateFormat.format(new Date());
        return date;
    }
    public static String getRiQi(){
        SimpleDateFormat sDateFormat = new SimpleDateFormat("dd");
        String date = sDateFormat.format(new Date());
        return date;
    }
    public static String getJ(){
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = sDateFormat.format(new Date());
        return date;
    }
    public static String getDate(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
        String date=sdf.format(new Date());
        return date;
    }
    public static String getDateYear(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy");
        String date=sdf.format(new Date());
        return date;
    }
    public static Date getT(String dateString){
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            date = sdf.parse(dateString);
        }
        catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        return date;
    }
    /**
     * 判断time是否在from，to之内
     * @return
     */
    public static boolean belongCalendar() {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date Crear= null;//开始
        Date from= null;//开始
        Date to=null;//结束
        try {
            Crear=getTime1();
            from = format.parse(getJ()+" 09:00:00");
            to = format.parse(getJ()+" 18:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String t3=format.format(Crear);
        String t1=format.format(from);
        String t2=format.format(to);
        Calendar date = Calendar.getInstance();
        date.setTime(Crear);

        Calendar after = Calendar.getInstance();
        after.setTime(from);

        Calendar before = Calendar.getInstance();
        before.setTime(to);

        if (date.after(after) && date.before(before)) {
            return true;
        } else {
            return false;
        }
    }
    public static Date getTime1(){
        long time=System.currentTimeMillis();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1=new Date(time);
        String t1=format.format(d1);
        Log.e("msg", t1);
        return d1;
    }
    public static Date getTs(String dateString){
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.parse(dateString);
        }
        catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        return date;
    }
    /**
     * 时间格式化
     * wuyuan 20170615
     *
     * @param time
     * @return
     */
    public static String getDatetimeFormat(long time){
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String datetime = sdf.format(date);
        datetime = datetime.replaceAll("-",".");
        return datetime;
    }
    public static String getDatetimeFormats(long time){
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datetime = sdf.format(date);
        datetime = datetime.replaceAll("-",".");
        return datetime;
    }
    /**
     * 时间格式化
     * wuyuan 20170615
     *
     * @param time
     * @return
     */
    public static String getDatetimeFormatvewvev(long time){
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String datetime = sdf.format(date);
        return datetime;
    }
    /**
     * 时间格式化
     * wuyuan 20170615
     *
     * @param time
     * @return
     */
    public static String getDatetimegetCade(long time){
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        String datetime = sdf.format(date);
        datetime = datetime.replaceAll("-",".");
        return datetime;
    }
    /**
     * 时间格式化
     * wuyuan 20170615
     *
     * @param time
     * @return
     */
    public static String getDatetimeNY(long time){
        String datetime="未知";
        if (time!=0){
            Date date = new Date(time);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            datetime = sdf.format(date);
        }
        return datetime;
    }
    public static String getDatetimeS(long time){
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String datetime = sdf.format(date);
        return datetime;
    }
    public static String getTString(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String datasafd=sdf.format(date);
        return datasafd;
    }
    public static String getMonthString(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
        String datasafd=sdf.format(date);
        return datasafd;
    }
    public static String getTStrings(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String datasafd=sdf.format(date);
        return datasafd;
    }
    /**
     * 将对象转换为JSON字符串
     */
    public static String toJsonString(Object value) {
        Gson gson = new Gson();
        return gson.toJson(value);
    }

    /**
     * 将JSON字符串转换为对象
     *
     * @param val
     * @param type
     * @return
     */
    public static <T> T fromJson(String val, Class<T> type) {
        Gson gson = new Gson();
        return gson.fromJson(val, type);
    }

    /**
     * 将JSON字符串转换为集合
     *
     * @param json
     * @param clazz
     * @return
     */
    public static <T> ArrayList<T> jsonToArrayList(String json, Class<T> clazz) {
        Type type = new TypeToken<ArrayList<JsonObject>>() {
        }.getType();
        ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json, type);

        ArrayList<T> arrayList = new ArrayList<>();
        for (JsonObject jsonObject : jsonObjects) {
            arrayList.add(new Gson().fromJson(jsonObject, clazz));
        }
        return arrayList;
    }
    private static final int seconds_of_1minute = 60;//1分钟
    private static final int seconds_of_30minutes = 30 * 60;//30分钟
    private static final int seconds_of_1hour = 60 * 60;//1小时
    private static final int seconds_of_1day = 24 * 60 * 60;//24小时（1天）
    private static final int seconds_of_15days = seconds_of_1day * 15;//半个月
    private static final int seconds_of_30days = seconds_of_1day * 30;//1个月
    private static final int seconds_of_6months = seconds_of_30days * 6;//半年
    private static final int seconds_of_1year = seconds_of_30days * 12;//1年
    /**
     * 计算评论数据的
     * 1小时内 mm分钟前
     * 1天内  HH:mm
     * 1天以前 昨天 HH:mm
     * 2天以前-1年之内 MM月dd日  HH:mm
     * 1年前 yyyy年MM月dd日
     */
    public static String getTimeElapse(long timeline){
        try {
            long nowTime = new Date().getTime()/1000;
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date begin = df.parse(timeline);

            //createTime是发表文章的时间
//            Date dateNow = new Date(timeline);

            long oldTime = timeline;

            //elapsedTime是发表和现在的间隔时间

            long elapsedTime = nowTime - oldTime;


//            if (elapsedTime < seconds_of_1minute) {//1分钟
//
//                return "";
//            }
//            if (elapsedTime > seconds_of_30minutes) {//30分钟内
//
//                return elapsedTime / seconds_of_1minute + "分钟前";
//            }
            if (elapsedTime < seconds_of_1hour) {//1小时

                return elapsedTime / seconds_of_1minute + "分钟前";
            }
            if (elapsedTime < seconds_of_1day) {//24小时（1天内）
                if(!getDatetimeS(new Date().getTime()).equals(getDatetimeS(timeline*1000))){//月日对比是否是今天的时间
                    return "昨天"+getTStrings(new Date(timeline*1000));
                }else{
                    return getTStrings(new Date(timeline*1000));
                }

            }
            if (elapsedTime < (seconds_of_1day*2)) {//48小时（2天内）

                return "昨天"+getTStrings(new Date(timeline*1000));
            }
            if (elapsedTime < seconds_of_15days) {//半个月

                return getMonthString(new Date(timeline*1000));
            }
            if (elapsedTime < seconds_of_30days) {//1个月

                return getMonthString(new Date(timeline*1000));
            }
            if (elapsedTime < seconds_of_6months) {//半年

                return getMonthString(new Date(timeline*1000));
            }
            if (elapsedTime < seconds_of_1year) {//1年内

                return getMonthString(new Date(timeline*1000));
            }
            if (elapsedTime >= seconds_of_1year) {//大于1年

                return getDatetimeS(timeline*1000);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return "";
    }



}
